package com.minutemouse.generator;

import java.util.ArrayList;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.FontMetrics;
import android.graphics.Paint.Style;
import android.graphics.Path;
import android.graphics.Rect;
import android.graphics.Typeface;
import android.text.Layout;
import android.text.StaticLayout;
import android.text.TextPaint;
import android.widget.Toast;

public class Generator {

	private String name = "Example";
	private String type = "At will";
	private String attributes = "Radiant, Weapon";
	private String actionType = "Standard Action";
	private String range = "Melee weapon";
	private String target = "";
	private String attack = "";
	private String hit = "";
	private String effect = "Effect: s";
	private String effect2 = "";
	private String effect3 = "";
	private int width = 480;
	private int height = 150;
	private Bitmap img;
	private Context genContext;
	
	public Generator(Context context) {
		this.genContext = context;
	}
	
	public void setValues(ArrayList<String> alist) {
		name = alist.get(0);
		type = alist.get(7);
		attributes = alist.get(1);
		actionType = alist.get(8);
		range = alist.get(2);
		target = alist.get(3);
		attack = alist.get(4);
		hit = alist.get(5);
		effect = alist.get(6);
		
	}
	
	public void setName(String nm) {
		name = nm;
	}
	
	public void setType(String tp) {
		type = tp;
	}
	
	public void setAttributes(String att) {
		attributes = att;
	}
	
	public void setActionType(String at) {
		actionType = at;
	}
	
	public void setRange(String rng) {
		range = rng;
	}
	
	public void setTarget(String tgt) {
		target = tgt;
	}
	
	public void setAttack(String atk) {
		attack = atk;
	}
	
	public void setHit(String ht) {
		hit = ht;
	}
	
	public void setEffect1(String eft) {
		effect = eft;
	}
	
	public void setEffect2(String eft2) {
		effect2 = eft2;
	}
	
	public void setEffect3(String eft3) {
		effect3 = eft3;
	}
	
	public Bitmap getImage() {
		generateImg();
		return img;
	}
	
	public Bitmap generateImg() {
		
		int newHeight = 0;
		
		Bitmap.Config conf = Bitmap.Config.ARGB_8888;
		Bitmap temp = Bitmap.createBitmap(width, height+200, conf);
		Canvas c = new Canvas(temp);
		
		c.drawColor(Color.rgb(227, 232, 169)); // Fills background.
			
		// Draw the green label.
		Rect box = new Rect(0,0,width,25);
		Paint boxpaint = new Paint();
		boxpaint.setColor(Color.GREEN);
		
		c.drawRect(box, boxpaint);
		
		
		
		// Set up paints for text.
		
		Paint boldText = new Paint();
		boldText.setColor(Color.BLACK);
		Paint nullText = new Paint(); // Normal text.
		nullText.setColor(Color.BLACK);
		
		// Make it bold.
		boldText.setTypeface(Typeface.DEFAULT_BOLD);
		
		// Set up our metrics.
		FontMetrics boldMetrics = boldText.getFontMetrics();
		FontMetrics nullMetrics = nullText.getFontMetrics();
		
		// Draw name
		c.drawText(name, 10, 23, boldText);
		
		// Draw attributes
		c.drawText(attributes, width - boldText.measureText(attributes) 
				- boldText.measureText(type) - 40 , 23, boldText);
		
		// Draw diamond.
		
		int leftside = (int) (width -  boldText.measureText(type) - 35);
		int rightside = leftside + 10;
		int top = 11;
		int bottom = 21;
		int middle = 16;
		int xmid = leftside + 5;
		int[] polyX = {leftside, xmid,  rightside, xmid};
		int[] polyY = {middle,   top,   middle,    bottom};
		
		Paint ppaint = new Paint();
		Path p = new Path();
		ppaint.setColor(Color.BLACK);
		ppaint.setStyle(Style.FILL);
		p.moveTo(polyX[0], polyY[0]);
		p.lineTo(polyX[1], polyY[1]);
		p.lineTo(polyX[2], polyY[2]);
		p.lineTo(polyX[3], polyY[3]);
		
		c.drawPath(p, ppaint);
		
		// Draw attack type
		c.drawText(type, width - nullText.measureText(type) - 20, 23, boldText);
		
		// Draw action type
		c.drawText(actionType, 10, 40, boldText);
		
		// Draw range
		
		c.drawText(range, width - boldText.measureText(range) - 18, 40, boldText);
		
		// Draw target
		
		c.drawText("Target: ", 10, 60, boldText);
		c.drawText(target, nullText.measureText("Target: ") + 20, 60, nullText);
		
		// Draw attack
		c.drawText("Attack: ", 10, 80, boldText);
		c.drawText(attack, nullText.measureText("Attack: ")+ 20, 80, nullText);
		
		// Draw hit
		c.drawText("Hit: ", 10, 100, boldText);
		c.drawText(hit, nullText.measureText("Hit: ") + 20, 100, nullText);
		
		String message = "This is a really long message that is created to text the limits of this so called" +
				"Static layout sl thingamajig that is supposed to help me out a royal lot.......";
		
		try {
		String[] toBeBold = effect.split(" ", 2);
		
		c.drawText(toBeBold[0], 10, 120, boldText);
	
		
		TextPaint tp = new TextPaint(Color.BLACK);
		
		int eDist = (int) boldText.measureText(toBeBold[0]) + 15; // Distance to offset the effect text.
		//TODO: Make this not hardcoded so different "effects" have appropriate spacing.
		c.translate(eDist, 109);
		
		StaticLayout sl = new StaticLayout(toBeBold[1], tp, c.getWidth()-eDist -10, Layout.Alignment.ALIGN_NORMAL, 
				1.0f, 1.0f, false);
		sl.draw(c);
		
		c.translate(-eDist, -110);
		
		
		//int height = sl.getHeight();
		
		//c.drawLine(0, height, width, height, nullText);
		

		// Draw border, after everything is set.
		Paint borderpaint = new Paint();
		borderpaint.setColor(Color.BLACK);
		borderpaint.setStyle(Style.STROKE);
		borderpaint.setStrokeWidth(1);
		Rect borderbox = new Rect(0,0,width-1, 120 + sl.getHeight());

		c.drawRect(borderbox, borderpaint);

		} catch (ArrayIndexOutOfBoundsException e) {
			/*
			 * In case the user doesn't put an effect, or a malformed effect (no spaces)
			 * the program will return a card of normal size and let them know it was a bad
			 * input. The card still generates, without barfing, so that's good. This 
			 * is the desired function
			 */
			Toast.makeText(genContext, "Bad input", Toast.LENGTH_SHORT).show();
			img = Bitmap.createBitmap(temp, 0,0, width, height);
			return img;
		}
		
		
		
		for (int k = c.getHeight()-1; k > 0; k --) {
			if (temp.getPixel(1, k) == Color.BLACK) {
				newHeight = k + 1;
				break;
			}
		}
		
		img = Bitmap.createBitmap(temp, 0,0, width, newHeight);
		
		return img;
	}
	
}
